package net.aaa.hotel.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import net.aaa.hotel.dao.DeptDao;
import net.aaa.hotel.dao.HRoleDao;
import net.aaa.hotel.dao.YGDao;
import net.aaa.hotel.dao.impl.DeptDaoImpl;
import net.aaa.hotel.dao.impl.HRoleDaoImpl;
import net.aaa.hotel.dao.impl.IYGDaoImpl;
import net.aaa.hotel.entity.HYg;
import net.aaa.hotel.util.LayuiTable;
import net.aaa.hotel.util.TransBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@RestController
@RequestMapping("/YG")
public class YGSpringController {
    @Autowired
    private YGDao dao = new IYGDaoImpl();
    @Autowired
    private HRoleDao daoR = new HRoleDaoImpl();
    @Autowired
    private DeptDao daoD = new DeptDaoImpl();

    @RequestMapping("/quer")
    public Map xinxi(HttpServletRequest req) {
        HttpSession session = req.getSession();
        String student = session.getAttribute("uname") + "";
        List<Map> map = dao.queryMing(student);
        Map map1 = map.get(0);
        session.setAttribute("url", map1.get("imgpath"));
        session.setAttribute("id", map1.get("id"));
        return map1;
    }

    @RequestMapping("/upl")
    public Map getUploadPath(MultipartFile file) {
        String path = "";
        Map ma = new HashMap();

        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        UUID uuid = UUID.randomUUID();
        String objectName = uuid + suffix;
        try {
            // Endpoint以杭州为例，其它Region请按实际情况填写。
            //
            //oss-cn-beijing.aliyuncs.com
            String endpoint = "oss-cn-beijing.aliyuncs.com";
            // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
            String accessKeyId = "LTAI4GCzjhtjNLVfjx8BPm1h";
            String accessKeySecret = "dmXsWFjrdNanypvoLXYuDsuO56slhP";
            String bucketName = "yuyongli";


            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            // 上传文件流。
            InputStream inputStream = file.getInputStream();
            PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, inputStream);

            // 关闭OSSClient。
            ossClient.shutdown();

            path = "https://" + bucketName + "." + endpoint + "/" + objectName;
            //文件上传需要的参数  msg code data
            ma.put("code", 0);
            ma.put("msg", "");
            ma.put("data", path);


        } catch (IOException e) {
            e.printStackTrace();

        }

        return ma;
    }

    @RequestMapping("/upd")
    public int updYg(HYg yg) {
        int i = dao.addImg(yg);
        if (i > 0) {
            return 1;
        }
        return 0;
    }

    @RequestMapping("/updM")

    public int updM(HYg yg) {
        System.out.println(yg.getId() + "" + yg.getPwd());
        int i = dao.updM(yg);
        if (i > 0) {
            return 1;

        }
        return 0;
    }

    @RequestMapping("/queryAllTypeAndMoney")
    public Map<String, List> queryAllTypeAndMoney() {
        List<Map> maps = dao.queryType();
        List<String> name = new ArrayList<>();
        Map<String, List> mapList = new HashMap<>(64);
        for (Map map : maps) {

            List<Double> value = dao.queryMoney(map);
            name.add(map.get("name")+"");
            mapList.put((map.get("bieming")+""),value);

        }
        List<Object> value = dao.queryMouth();
        mapList.put("name", name);
        mapList.put("monthData", value);

        return mapList;
    }
    @RequestMapping("/AllTypeAndMoney")
    public LayuiTable AllTypeAndMoney(){
        List<Map> maps =dao.queryType();
        List<Map> maps1 = new ArrayList<>();

        for (Map map : maps) {
           Map map1 = dao.qeuryY(map);
           map1.put("name",map.get("name"));
           maps1.add(map1);

        }
        LayuiTable layuiTable = new LayuiTable(maps1.size(), maps1);


        return layuiTable;
    }
}
